package game;

import game.components.Cordinent;
import game.components.Player;
import game.components.StartingPosition;
import game.components.squares.BasicSquare;
import game.components.squares.EmptySquare;
import game.components.squares.GamePiece;
import game.components.squares.WaterSquare;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

public class Game
{
	private Player[]		players;
	private BasicSquare[][]	board;

	public Game(Player player1, Player player2)
	{
		players = new Player[] { player1, player2 };
		initializeBoard();

	}

	public void addMouseListenerToPieces(MouseListener l)
	{
		for (BasicSquare[] a : board)
		{
			for (BasicSquare s : a)
			{
				s.addMouseListener(l);
			}
		}
	}

	public BasicSquare[][] getBoard()
	{
		return board;
	}

	public Player getPlayer(int index)
	{
		if (index < 0 || index >= players.length){ return null; }
		return players[index];
	}

	public Player getPlayer(String playerName)
	{
		for (Player p : players)
		{
			if (p.getName().equals(playerName)){ return p; }
		}
		return null;
	}

	public List<Cordinent> getPossibleMoves(BasicSquare s)
	{

		BasicSquare temp = null;
		List<Cordinent> possibleMoves = new ArrayList<Cordinent>();

		if (s.getMovable() && ( s instanceof GamePiece ))
		{
			GamePiece p = (GamePiece) s;

			int checkValue;// The value to check until

			// DOWN
			if (!p.getMovableMultipleSquares())
			{
				if (p.getBoardYLocation() < 9)
				{
					checkValue = p.getBoardYLocation() + 1;
				}
				else
				{
					checkValue = 9;
				}
			}
			else
			{
				checkValue = 9;
			}

			for (int i = p.getBoardYLocation() + 1; i <= checkValue; i++)
			{
				temp = board[p.getBoardXLocation()][i];
				if (temp instanceof EmptySquare)
				{
					possibleMoves.add(new Cordinent(temp.getBoardXLocation(),
							temp.getBoardYLocation()));
				}
				else if (temp instanceof WaterSquare)
				{
					break;
				}
				else if (temp instanceof GamePiece)
				{
					GamePiece p2 = (GamePiece) temp;
					if (!p2.getColor().equals(p.getColor()))
					{
						possibleMoves.add(new Cordinent(p2.getBoardXLocation(),
								p2.getBoardYLocation()));
					}
					break;
				}
			}

			// UP
			if (!p.getMovableMultipleSquares())
			{
				if (p.getBoardYLocation() > 0)
				{
					checkValue = p.getBoardYLocation() - 1;
				}
				else
				{
					checkValue = 0;
				}
			}
			else
			{
				checkValue = 0;
			}

			for (int i = p.getBoardYLocation() - 1; i >= checkValue; i--)
			{
				temp = board[p.getBoardXLocation()][i];
				if (temp instanceof EmptySquare)
				{
					possibleMoves.add(new Cordinent(temp.getBoardXLocation(),
							temp.getBoardYLocation()));
				}
				else if (temp instanceof WaterSquare)
				{
					break;
				}
				else if (temp instanceof GamePiece)
				{
					GamePiece p2 = (GamePiece) temp;
					if (!p2.getColor().equals(p.getColor()))
					{
						possibleMoves.add(new Cordinent(p2.getBoardXLocation(),
								p2.getBoardYLocation()));
					}
					break;
				}
			}

			// RIGHT
			if (!p.getMovableMultipleSquares())
			{
				if (p.getBoardXLocation() < 9)
				{
					checkValue = p.getBoardXLocation() + 1;
				}
				else
				{
					checkValue = 9;
				}
			}
			else
			{
				checkValue = 9;
			}

			for (int i = p.getBoardXLocation() + 1; i <= checkValue; i++)
			{
				temp = board[i][p.getBoardYLocation()];
				if (temp instanceof EmptySquare)
				{
					possibleMoves.add(new Cordinent(temp.getBoardXLocation(),
							temp.getBoardYLocation()));
				}
				else if (temp instanceof WaterSquare)
				{
					break;
				}
				else if (temp instanceof GamePiece)
				{
					GamePiece p2 = (GamePiece) temp;
					if (!p2.getColor().equals(p.getColor()))
					{
						possibleMoves.add(new Cordinent(p2.getBoardXLocation(),
								p2.getBoardYLocation()));
					}
					break;
				}
			}

			// LEFT
			if (!p.getMovableMultipleSquares())
			{
				if (p.getBoardXLocation() > 0)
				{
					checkValue = p.getBoardXLocation() - 1;
				}
				else
				{
					checkValue = 0;
				}
			}
			else
			{
				checkValue = 0;
			}

			for (int i = p.getBoardXLocation() - 1; i >= checkValue; i--)
			{
				temp = board[i][p.getBoardYLocation()];
				if (temp instanceof EmptySquare)
				{
					possibleMoves.add(new Cordinent(temp.getBoardXLocation(),
							temp.getBoardYLocation()));
				}
				else if (temp instanceof WaterSquare)
				{
					break;
				}
				else if (temp instanceof GamePiece)
				{
					GamePiece p2 = (GamePiece) temp;
					if (!p2.getColor().equals(p.getColor()))
					{
						possibleMoves.add(new Cordinent(p2.getBoardXLocation(),
								p2.getBoardYLocation()));
					}
					break;
				}
			}

		}

		return possibleMoves;

	}

	private void initializeBoard()
	{
		board = new BasicSquare[10][10];

		// make all squares empty
		for (int i = 0; i < board.length; i++)
		{
			for (int j = 0; j < board[i].length; j++)
			{
				board[i][j] = new EmptySquare(new Cordinent(i, j));
			}
		}

		// add lakes
		board[2][4] = new WaterSquare(new Cordinent(2, 4));
		board[3][4] = new WaterSquare(new Cordinent(3, 4));
		board[2][5] = new WaterSquare(new Cordinent(2, 5));
		board[3][5] = new WaterSquare(new Cordinent(3, 5));

		board[6][4] = new WaterSquare(new Cordinent(6, 4));
		board[7][4] = new WaterSquare(new Cordinent(7, 4));
		board[6][5] = new WaterSquare(new Cordinent(6, 5));
		board[7][5] = new WaterSquare(new Cordinent(7, 5));

		Player top;
		Player bottom;
		if (players[0].getStartingPosition().equals(StartingPosition.Top))
		{
			top = players[0];
			bottom = players[1];
		}
		else
		{
			top = players[1];
			bottom = players[0];
		}

		// TOP PLAYER

		board[0][0] = new GamePiece(new Cordinent(0, 0), top, 0);// flag(0)
		for (int i = 1; i < 7; i++)// bombs(11)
		{
			board[i][0] = new GamePiece(new Cordinent(i, 0), top, 11);// bomb
		}

		board[7][0] = new GamePiece(new Cordinent(7, 0), top, 1);// Spy(1)

		for (int i = 8; i < 10; i++)// 3s
		{
			board[i][0] = new GamePiece(new Cordinent(i, 0), top, 3);// 3
		}
		for (int i = 0; i < 3; i++)// 3s
		{
			board[i][1] = new GamePiece(new Cordinent(i, 1), top, 3);// 3
		}

		for (int i = 3; i < 7; i++)// 4s
		{
			board[i][1] = new GamePiece(new Cordinent(i, 1), top, 4);// 4
		}

		for (int i = 7; i < 10; i++)// 5s
		{
			board[i][1] = new GamePiece(new Cordinent(i, 1), top, 5);// 5
		}
		board[0][2] = new GamePiece(new Cordinent(0, 2), top, 5);// 5

		for (int i = 1; i < 5; i++)// 6s
		{
			board[i][2] = new GamePiece(new Cordinent(i, 2), top, 6);// 6
		}

		for (int i = 5; i < 8; i++)// 7s
		{
			board[i][2] = new GamePiece(new Cordinent(i, 2), top, 7);// 7
		}

		for (int i = 8; i < 10; i++)// 8s
		{
			board[i][2] = new GamePiece(new Cordinent(i, 2), top, 8);// 8
		}

		board[0][3] = new GamePiece(new Cordinent(0, 3), top, 9);// 9
		board[1][3] = new GamePiece(new Cordinent(1, 3), top, 10);// 10

		for (int i = 2; i < 10; i++)// 2s
		{
			board[i][3] = new GamePiece(new Cordinent(i, 3), top, 2);// 2
		}

		// BOTTOM PLAYER

		for (int i = 0; i < 8; i++)// 2's
		{
			board[i][6] = new GamePiece(new Cordinent(i, 6), bottom, 2);// 2
		}

		board[8][6] = new GamePiece(new Cordinent(8, 6), bottom, 10);// 10
		board[9][6] = new GamePiece(new Cordinent(9, 6), bottom, 9);// 9

		for (int i = 0; i < 2; i++)// 8's
		{
			board[i][7] = new GamePiece(new Cordinent(i, 7), bottom, 8);// 8
		}

		for (int i = 2; i < 5; i++)// 7's
		{
			board[i][7] = new GamePiece(new Cordinent(i, 7), bottom, 7);// 7
		}

		for (int i = 5; i < 9; i++)// 6's
		{
			board[i][7] = new GamePiece(new Cordinent(i, 7), bottom, 6);// 6
		}

		board[9][7] = new GamePiece(new Cordinent(9, 7), bottom, 5);// 5
		for (int i = 0; i < 3; i++)// 5's
		{
			board[i][8] = new GamePiece(new Cordinent(i, 8), bottom, 5);// 5
		}

		for (int i = 3; i < 7; i++)// 4's
		{
			board[i][8] = new GamePiece(new Cordinent(i, 8), bottom, 4);// 4
		}

		for (int i = 7; i < 10; i++)// 3's
		{
			board[i][8] = new GamePiece(new Cordinent(i, 8), bottom, 3);// 3
		}
		for (int i = 0; i < 2; i++)// 3's
		{
			board[i][9] = new GamePiece(new Cordinent(i, 9), bottom, 3);// 3
		}

		board[2][9] = new GamePiece(new Cordinent(2, 9), bottom, 1);// Spy(1)

		for (int i = 3; i < 9; i++)// Bomb's(11)
		{
			board[i][9] = new GamePiece(new Cordinent(i, 9), bottom, 11);// Bomb(11)
		}

		board[9][9] = new GamePiece(new Cordinent(9, 9), bottom, 0);// Flag(0)

	}

}
